//
//  HomeCommonPage.m
//  Pictorial
//
//  Created by SKY on 15/12/28.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "HomeCommonPage.h"
#import "ShareView.h"
#import "ShareEditView.h"
#import "HomePictureScrollView.h"
#import "HomeNavBar.h"

#import <UIImageView+WebCache.h>

#define INTERVAL 10

@interface HomeCommonPage ()<UIScrollViewDelegate,HomeNavBarDelegate,ShareViewDelegate,ShareEditViewDelegate>
{
    CGFloat _maxHeight;
}
@property (weak, nonatomic) IBOutlet UIImageView *picture;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomValue;

@property (nonatomic,weak) HomeNavBar *navBar;
@property (strong, nonatomic) UILabel *pictureLabel;
@property (strong, nonatomic) UILabel *textLabel;

@end

@implementation HomeCommonPage

-(void)awakeFromNib{
    

}
-(void)willMoveToSuperview:(UIView *)newSuperview{
    self.delegate = self;
}
-(void)addNavBar{
    
    HomeNavBar *navBar = [[[NSBundle mainBundle] loadNibNamed:@"HomeNavBarView" owner:nil options:nil] firstObject];
    [self.superview addSubview:navBar];
        
    navBar.delegate = self;
    self.navBar = navBar;
    
    self.navBar.frame = CGRectMake(self.frame.origin.x, 0, WIDTH, 50);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.navBar.navBar.alpha = scrollView.contentOffset.y / 400.0;
}
-(void)more{
    [_moreDelegate more];
}
-(void)share{
    
    ShareView *shareView = [[[NSBundle mainBundle] loadNibNamed:@"SharePageView" owner:nil options:nil] firstObject];
    [self.superview.superview addSubview:shareView];
    shareView.delegate = self;
    
}
-(void)shareWithType:(NSInteger)type{
    
    ShareEditView *shareEditView = [ShareEditView nibInitializtion];
    [self addSubview:shareEditView];
    
    shareEditView.content = [NSString stringWithFormat:@"来足迹画报，领略各地风景----\n%@",self.model.title];
    shareEditView.picture = self.model.image_url;
    shareEditView.type = type;
    shareEditView.delegate = self;
}
-(void)shareOnPlatform:(NSInteger)type content:(NSString *)content{
    [_shareDelegate shareWithContent:content picture:self.model.image_url platform:type];
}
-(void)downLoad:(UITapGestureRecognizer *)tap{
    
    NSArray *pictureArray = self.model.photos;
    
    HomePictureScrollView *pictureScrollView = [HomePictureScrollView classInitializtion];
    [self.superview.superview addSubview:pictureScrollView];
    pictureScrollView.title = self.model.title;
    pictureScrollView.desc = self.model.trip[@"name"];
    pictureScrollView.pictures = pictureArray;
    pictureScrollView.index = tap.view.tag - 100;
}
-(void)setModel:(HomeCommonModel *)model{
    _model = model;
    
    [self.picture imageLoadProgressWithNetImage:model.image_url];
    self.titleLabel.text = model.title;
    self.contentLabel.text = model.desc;
    [self.contentLabel layoutIfNeeded];
    
    NSArray *pictureArray = model.photos;

    CGFloat interval = INTERVAL;
    CGFloat x = interval;
    CGFloat y = CGRectGetMaxY(self.contentLabel.frame) + interval;
    CGFloat w = (WIDTH - interval * 3) * 0.5;
    CGFloat h = w;
    _maxHeight = 0;

    for(int index = 0; index < pictureArray.count; index ++){
        
        NSDictionary *dict = pictureArray[index];
        
        UIImageView *imageView = [UIImageView imageViewWithNetImage:dict[@"image_url"] frame:CGRectMake(x, y, w, h)];
        [self addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        imageView.userInteractionEnabled = YES;
        imageView.tag = index + 100;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downLoad:)];
        [imageView addGestureRecognizer:tap];
        
        index % 2 == 1 ? (x = interval, y += interval + h):(x += interval + w);
        
        if (index == (pictureArray.count - 1)) {
            _maxHeight = CGRectGetMaxY(imageView.frame) + 20;
        }
    }
    self.pictureLabel.text = [NSString stringWithFormat:@"图:%@",model.photo_author];
    self.textLabel.text = [NSString stringWithFormat:@"文:%@",model.text_author];
    
    self.bottomValue.constant = CGRectGetMaxY(self.textLabel.frame) - CGRectGetMaxY(self.contentLabel.frame) + 30;
    [self.contentLabel layoutIfNeeded];
    
    self.contentSize = CGSizeMake(WIDTH, CGRectGetMaxY(self.textLabel.frame));
}
-(UILabel *)textLabel{
    if (_textLabel == nil) {
        _textLabel = [UILabel labelWithText:@"" fontSize:15 frame:CGRectMake(INTERVAL, _maxHeight + 20 + 10, WIDTH - INTERVAL * 2, 20) color:[UIColor grayColor] textAlignment:0];
        [self addSubview:_textLabel];
    }
    return _textLabel;
}
-(UILabel *)pictureLabel{
    if (_pictureLabel == nil) {
        _pictureLabel = [UILabel labelWithText:@"" fontSize:15 frame:CGRectMake(INTERVAL, _maxHeight, WIDTH - INTERVAL * 2, 20) color:[UIColor grayColor] textAlignment:0];
        [self addSubview:_pictureLabel];
    }
    return _pictureLabel;
}
@end
